package com.seventh.parkingrent.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seventh.parkingrent.Util.EncryptUtils;
import com.seventh.parkingrent.commens.Result;
import com.seventh.parkingrent.controller.form.PropertyManagerForm;
import com.seventh.parkingrent.dao.PropertyManagerMapper;
import com.seventh.parkingrent.domain.PropertyManager;
import com.seventh.parkingrent.service.IPropertyManagerService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-08-25
 */
@Service
public class PropertyManagerServiceImpl extends ServiceImpl<PropertyManagerMapper, PropertyManager> implements IPropertyManagerService {
    @Resource
    private PropertyManagerMapper propertyManagerMapper;

    @Override
    public PropertyManager selectPropertyManagerById(Integer propertyManagerId) {
        PropertyManager propertyManager = propertyManagerMapper.selectById(propertyManagerId);
        return propertyManager;
    }

    @Override
    public List<String> selectPropertyManagerName() {
        List<String> propertyManagerNames = propertyManagerMapper.selectPropertyManagerNames();
        return propertyManagerNames;
    }

    @Override
    public Result register(PropertyManagerForm form) throws NoSuchAlgorithmException {
        @NotBlank String propertyManagerName = form.getPropertyManagerName();
        QueryWrapper<PropertyManager> wrapper = new QueryWrapper<>();
        wrapper.eq("property_manager_name",propertyManagerName);
        PropertyManager one = propertyManagerMapper.selectOne(wrapper);
        if(null!=one){
            return Result.fail("账户名已存在");
        }

        @NotBlank String propertyName = form.getPropertyName();
        QueryWrapper<PropertyManager> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("property_name",propertyName);
        PropertyManager one1 = propertyManagerMapper.selectOne(wrapper1);
        if(null!=one1){
            return Result.fail("物业名已存在");
        }

        PropertyManager propertyManager = new PropertyManager();
        @NotBlank String originPassword = form.getPropertyManagerPassword();
        String password = EncryptUtils.encrypt(originPassword);
        form.setPropertyManagerPassword(password);
        BeanUtils.copyProperties(form,propertyManager);
        int insert = propertyManagerMapper.insert(propertyManager);
        if(insert>0){
            return Result.success("注册成功");
        }

        return  Result.fail("物业注册失败，发生了预料外的情况");
    }
}
